<template>
  <div>
    <el-dialog :title="title" :visible.sync="visible" append-to-body>
      <!-- 主表表单 -->
      <el-form ref="form" :model="model" :rules="rules" label-width="100px">
        <el-row :gutter="2">
          <el-col :span="span">
            <el-form-item label="编码" prop="code">
              <el-input v-model="model['code']" placeholder="编码" :readonly="!canEdit"/>
            </el-form-item>
          </el-col>
          <el-col :span="span">
            <el-form-item label="名称" prop="name">
              <el-input v-model="model['name']" placeholder="名称" :readonly="!canEdit"/>
            </el-form-item>
          </el-col>
          <el-col :span="span">
            <el-form-item label="标准工时" prop="stdTime">
              <el-input v-model="model['stdTime']" placeholder="标准工时" :readonly="!canEdit"/>
            </el-form-item>
          </el-col>
          <el-col :span="span">
            <el-form-item label="工价" prop="priceOne">
              <el-input v-model="model['priceOne']" placeholder="工价" :readonly="!canEdit"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <!-- 子表列表 -->
      <el-tabs v-model="active" v-if="tabs.length>0" @tab-click="onChangeTab">
        <el-tab-pane label="工序活动" name="actions">
          <process-action ref="actions" :url="subUrl" :row-key="rowKey"
                          :columns="tabs[0].columns" :can-edit="canEdit"/>
        </el-tab-pane>
      </el-tabs>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submit">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {FFormMixin} from "@/minxins/form/FFormMixin";
import ProcessAction from "@/views/core/route/process/dialog/processAction";

export default {
  name: "index",
  components: {ProcessAction},
  mixins: [FFormMixin],
  props: {
    url: {
      type: Object,
      default: () => {
        return {};
      }
    },
    col: {
      type: Number,
      default: 3
    },
    rowKey: {
      type: String,
      default: "id"
    },
    rules: {
      type: Object,
      default: () => {
        return {};
      }
    },
    tabs: {
      type: Array,
      default: () => {
        return [];
      }
    },
  },
  computed: {
    span: function () {
      return (24 / this.col)
    },
    subUrl: function () {
      return this.tabs.filter(item => {
        if (item.name === this.active) {
          return item;
        }
      })[0].url;
    },
  },
  data() {
    return {
      loadOnCreated: false,
      active: this.tabs[0].name || '',
      canEdit: true
    }
  },
  methods: {
    showMore: function () {
      const firstTab = this.tabs[0];
      this.$nextTick(() => {
        if (this.isEdit) {
          this.loadData(firstTab);
        } else {
          this.$refs[firstTab.name].data = [];
        }
      });
    },
    loadData: function (tab) {
      let filter = {};
      filter[tab.forKey] = this.model[this.rowKey];
      this.$refs[tab.name].filters = filter;
      this.$refs[tab.name].loadData({page: 1});
    },
    submit: function () {
      const _this = this;
      this.$refs.form.validate(valid => {
        if (valid) {
          _this.tabs.forEach(tab => {
            _this.model[tab.name] = _this.$refs[tab.name].data;
          });
          // console.log(_this.model);
          if (this.isEdit) {
            _this.edit();
          } else {
            _this.create();
          }
        }
      });
    },
    onChangeTab: function (tab) {
      if (this.isEdit) {
        this.loadData(tab);
      }
    }
  }
}
</script>

<style scoped>

</style>
